<?php

namespace App\Http\Controllers\GarbageCollect;

use App\Models\Garbage\GarbageDissminateNews;
use App\Models\Garbage\GarbagePoint;
use App\Models\Garbage\GarbageTestUser;
use App\Models\Garbage\WeichatUser;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use Illuminate\Database\QueryException;
use Illuminate\Support\Facades\Facade;
use Validator;
use App\Models\SystemManagement\User;

/**
 * @group 垃圾分类->会员管理->积分管理->积分查看页面
 *
 * 采用 restful api
 *
 * Body Parameters 表示请求的所需要的参数
 *
 * Query Parameters 表示返回参数的名称
 */

class GarbagePointController extends Controller
{
    /**
     * 信息展 bug(设备号暂无)
     * @queryParam no 流水编号.
     * @queryParam event_point 积分 有正有负.
     * @queryParam final_point 变动后用户的积分.
     * @queryParam event 变动事件.
     * @queryParam headimgurl 用户头像.
     * @queryParam openid 微信号
     * @queryParam nickname 昵称.
     * @queryParam accumulation_point 总积分.
     * @queryParam point 可用积分.
     * @queryParam garbage_post_num_count 投递次数.
     * @queryParam user 存放用户信息.
     * @queryParam user->card 卡号
     * @queryParam user->point 当前积分
     * @queryParam user->accumulation_point 累计积分
     *
     * @bodyParam user_id int required 用户id号 外键 用户表
     * @bodyParam no string optional 搜索流水号(模糊)
     * @bodyParam start_time datetime optional 搜索起始时间
     * @bodyParam end_time datetime optional 搜索结束时间
     */
    public function index(Request $request){
        $e = genValidate();
        $validator = Validator::make($request->all(),$e);
        if($validator->fails()){
            return response()->json(encodeMsg($validator->errors()->messages(),400),400);
        }
        $query = GarbagePoint::where('user_id','=',$request->get('user_id'));
        $query->where(function ($q) use ($request){
            $request->has('no') && $q->where('no','LIKE',"%{$request->get('no')}%");
            $request->has('start_time') && $q->where('created_at','>=',"{$request->get('start_time')}");
            $request->has('end_time') && $q->where('created_at','<=',"{$request->get('end_time')}");
        });
        $user = WeichatUser::withCount('GarbagePostNum')->find($request->get('user_id'));
        $res = array('user'=>$user,'data'=>$query->paginate($request->input('per_page',10)));
        return response()->json(encodeMsg($res,200),200);

    }
//
//    /**
//     * 信息添加
//     *
//     * @queryParam event_point 积分 有正有负.
//     * @queryParam final_point 变动后用户的积分.
//     * @queryParam event 变动事件.
//     * @queryParam user_id 用户id.
//     * @queryParam avatar_url 用户头像.
//     * @queryParam wechat_name 微信名.
//     * @queryParam alias 昵称.
//     * @queryParam accumulation_point 总积分.
//     * @queryParam point 可用积分.
//     *
//     *
//     * @bodyParam event string required 变动事件.
//     * @bodyParam event_point text required 积分 有正有负.
//     * @bodyParam user_id int required 用户id号.
//     */
//
//    public function store(Request $request)
//    {
//        try {
//            $validator = Validator::make($request->all(),genValidate());
//            if($validator->fails()){
//                return response()->json(encodeMsg($validator->errors()->messages(),400),400);
//            }
//            $p = new GarbagePoint();
//            $user_id = $request->get('user_id');
//            $event_point = $request->get('event_point');
//            $user = WeichatUser::find($user_id);
//            if($user == null){
//                return response()->json(['message'=>'没有该用户'],404);
//            }
//            $p->event_point = $event_point;
//            $p->user_id = $user_id;
//            $p->event = $request->get('event');
//            $user_point = $user->point;
//            $final_point = $user_point + $event_point;
//            $p->final_point=$final_point;
//            if($final_point < 0){
//                return response()->json(encodeMsg(["message" =>'变更后用户积分为负 不能变更'],400), 400);
//            }
//            if($event_point>0) {
//                $user->accumulation_point = $user->accumulation_point + $event_point;
//
//            }
//            $user->point=$final_point;
//            $user->save();
//            $p->no = genOrderNumber();
//            $p->save();
//            return response()->json(encodeMsg($p,201), 201);
//        } catch (QueryException $e) {
//            return response()->json(encodeMsg(["message" => $e->getMessage()],500), 500);
//        }
//    }


    /**
     * 单个信息展示
     * @queryParam no 流水编号.
     * @queryParam event_point 积分 有正有负.
     * @queryParam final_point 变动后用户的积分.
     * @queryParam event 变动事件.
     * @queryParam user_id 用户id.
     * @queryParam avatar_url 用户头像.
     * @queryParam wechat_name 微信名.
     * @queryParam alias 昵称.
     * @queryParam accumulation_point 总积分.
     * @queryParam point 可用积分.
     */
    public function show($pk)
    {
        $data = GarbagePoint::with('user')->find($pk);
        if ($data == []) {
            return response()->json(encodeMsg(["message" => "没有该信息"],404), 404);
        } else {
            return response()->json(encodeMsg($data,200), 200);
        }
    }

    /**
     * 信息删除
     */
    public function destroy($pk){
        $data = GarbagePoint::find($pk);
        if($data == []){
            return response()->json(encodeMsg(["message"=>"没有该信息"],404),404);
        }
        $data->delete();
        return response()->json(encodeMsg(["message"=>"删除成功"],200),200);
    }
}